<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="评估表名称">
          <el-input v-model="searchForm.evaluationName" clearable>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="add-button">添加</el-button>
          <el-button type="primary" @click="showTemplate" class="search-button">选择模板</el-button>
          <el-button @click="handleReset" class="search-button">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="评估表名称" align="center" prop="evaluationName">
        </el-table-column>
        <el-table-column label="评估表内容" align="center" prop="questionNums">
          <template slot-scope="{row}">
            <el-button @click="goContent(row)" type="text">{{ row.questionNums }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" align="center" prop="createTime">
        </el-table-column>
        <el-table-column label="录入人" align="center" prop="hospitalName">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="评估表名称">
          <el-input v-model.trim="form.evaluationName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="选择模板" :visible.sync="templateDailog" width="80%">
      <evaluationTemplate v-if="templateDailog" :evaluationType="'入户评估表'" @close="close"></evaluationTemplate>
    </el-dialog>
  </div>
</template>

<script>
import * as FieldEvaluation from '@/api/FieldEvaluation'
import evaluationTemplate from '@/components/evaluationTemplate'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination, evaluationTemplate },
  data () {
    return {
      list: null,
      listLoading: false,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      dialogTit: '',
      form: {},
      templateDailog: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      param.evaluationType = '入户评估表'
      param.hospitalUid = localStorage.getItem('hospitalUid')
      FieldEvaluation.queryByPage(param).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })
    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd () {
      this.dialogTit = '新增'
      this.form = {}
      this.dialogVisible = true
    },
    showEdit (row) {
      this.dialogTit = '编辑'
      this.form = Object.assign({}, row)
      delete this.form.createTime
      delete this.form.updateTime
      this.dialogVisible = true
    },
    submit () {
      if (this.dialogTit === '新增') {
        this.creatHandle()
      } else if (this.dialogTit === '编辑') {
        this.editHandle()
      }
    },
    creatHandle () {
      if (localStorage.getItem('hospitalUid') && localStorage.getItem('hospitalAme')) {
        this.form.hospitalUid = localStorage.getItem('hospitalUid')
        this.form.hospitalName = localStorage.getItem('hospitalAme')
      } else {
        this.$message.error('当前缓存失效，无法获取录入人信息，请重新登录！')
        return false
      }
      this.form.evaluationType = '入户评估表'
      FieldEvaluation.insert(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle () {
      FieldEvaluation.update(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        FieldEvaluation.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    goContent (data) {
      this.$router.push({
        path: '/SystemManagement/FieldEvaluationContent',
        query: {
          uid: data.uid
        }
      })
    },
    showTemplate () {
      this.templateDailog = true
    },
    close (msg) {
      if (msg === '') {
        this.templateDailog = false
      } else {
        let param = {
          hospitalUid: localStorage.getItem('hospitalUid'),
          hospitalName: localStorage.getItem('hospitalAme'),
          evaluationDOReq: {
            uid: msg.uid,
            evaluationName: msg.evaluationName,
            evaluationType: msg.evaluationType
          }
        }
        FieldEvaluation.insertTemplate(param).then(response => {
          if (response.code == 200) {
            this.templateDailog = false
            this.getList()
            this.$message.success('新增成功')
          }
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      }
    },
  }
}
</script>
